package model

import (
	"database/sql"
	pb "git.huoys.com/block_chain/mall/internal/user/api"
)

type Wallet struct {
	ID             uint64       `json:"id" gorm:"primaryKey;column:id;type:bigint auto_increment"`
	UID            uint64       `json:"uid" gorm:"column:uid;type:bigint;not null;index:wallet_uid_user_id_foreign;comment:用户id"` // 用户id
	ComputingPower float32      `json:"computing_power" gorm:"column:computing_power;type:float;not null;default:0;comment:算力"`   // 算力
	Addresses      string       `json:"addresses" gorm:"column:addresses;type:varchar(128);not null;comment:钱包地址"`                // 钱包地址
	CreatedAt      sql.NullTime `json:"created_at" gorm:"column:created_at;type:timestamp"`
	UpdatedAt      sql.NullTime `json:"updated_at" gorm:"column:updated_at;type:timestamp"`
}

// TableName returns the table name of the wallet model
func (w *Wallet) TableName() string {
	return "wallet"
}

type TransferRecord struct {
	ID      uint64               `json:"id" gorm:"primaryKey;column:id;type:bigint auto_increment"`
	FromUID uint64               `json:"from_uid" gorm:"column:from_uid;type:bigint;not null;comment:转出用户"`                          // 转出用户
	ToUID   uint64               `json:"to_uid" gorm:"column:to_uid;type:bigint;not null;comment:转入用户"`                              // 转入用户
	OrderID uint64               `json:"order_id" gorm:"column:order_id;type:bigint;not null;default:0;comment:商品id，购买商品的转账需要的商品id"` // 商品id，购买商品的转账需要的商品id
	Price   float32              `json:"price" gorm:"column:price;type:decimal(12,4);not null;comment:金额"`                           // 金额
	Hash    string               `json:"hash" gorm:"column:hash;type:varchar(128);not null;comment:交易哈希"`                            // 交易哈希
	Status  pb.TransactionStatus `json:"status" gorm:"column:status;type:tinyint;not null;default:1;comment:交易状态: 1交易中，2成功，3失败"`     // 交易状态: 1交易中，2成功，3失败
}

// TableName returns the table name of the TransferRecord model
func (t *TransferRecord) TableName() string {
	return "transfer_record"
}

type GoodsForOrder struct {
	ID   uint64 `json:"id" gorm:"primaryKey;column:id;type:bigint"`
	Name string `json:"name" gorm:"column:name;type:varchar(255);not null;comment:商品名称"` // 商品名称
}

type OrderTransferRecord struct {
	TransferRecord
	GoodsName string `json:"goods_name" gorm:"column:goods_name;type:varchar(255);not null;comment:商品名称"` // 商品名称
}

func (t TransferRecord) ToOrderTransferRecord(name string) (o OrderTransferRecord) {
	return OrderTransferRecord{
		TransferRecord: TransferRecord{
			ID:      t.ID,
			FromUID: t.FromUID,
			ToUID:   t.ToUID,
			OrderID: t.OrderID,
			Price:   t.Price,
			Hash:    t.Hash,
			Status:  t.Status,
		},
		GoodsName: name,
	}
}

func (w Wallet) ToPbWallet() (wallet *pb.Wallet) {
	wallet = &pb.Wallet{
		Wid:            w.ID,
		Addresses:      w.Addresses,
		ComputingPower: w.ComputingPower,
	}
	return wallet
}
